#!/usr/bin/env bash
# coding=utf-8
# Copyright (C) ATHENA AUTHORS
# All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

# score-computing stage,computing score with sclite
# source some path
. ./tools/env.sh

echo "$0 $@"  # Print the command line for logging

if [ $# -lt 3 ]; then
  echo "Usage: run_rover.sh <dest-hyp> <src-hyp1> <src-hyp2> ..."
  exit 1
fi

dest=$1;

shift;

export LC_ALL=C

for hyp in "$@"; do
  if [ ! -f $hyp ]; then
    echo "$0: no such file $hyp"
    exit 1;
  fi
done


for hyp in "$@"; do
  # replace hyp extension name to ctm extension name
  ctm_file=$(echo $hyp|sed 's/\.[a-zA-Z0-9]*$/\.ctm/')
#  ctm="$(dirname ${hyp})/$(basename ${hyp} .result).ctm"
  python examples/asr/aishell/local/hyp2ctm.py ${hyp} ${ctm_file}
  ctm_files+=" ${ctm_file}"
  _hyps+=" -h ${ctm_file} ctm"

done

dest_ctm=$(echo $dest|sed 's/\.[a-zA-Z0-9]*$/\.ctm/')
echo "./tools/SCTK/bin/rover ${_hyps} -o ${dest_ctm} -m meth1"
./tools/SCTK/bin/rover -f 0 ${_hyps} -o "${dest_ctm}" -m meth1

python examples/asr/aishell/local/ctm2hyp.py "${dest_ctm}" "${dest}"
rm ${ctm_files} ${dest_ctm}

#./tools/SCTK/bin/rover -f 0 -h examples/asr/misp/result/noise_conformer1214_avg20_attention_rescoring_ctc0_7_avg5_tlm0_5_beam20.ctm ctm \
#    -h examples/asr/misp/result/noise_conformer_attention_rescoring_ctc0_7_avg1_tlm0_5_beam20.ctm ctm -o test.ctm -m meth1